package org.hikari.qqbot.client.http;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.ParameterizedTypeImpl;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.hikari.qqbot.client.IMessageClient;
import org.hikari.qqbot.client.LLR;
import org.hikari.qqbot.client.dto.GroupMsgRequest;
import org.hikari.qqbot.client.dto.GroupMsgResponse;
import org.hikari.qqbot.client.dto.PrivateMsgRequest;
import org.hikari.qqbot.client.dto.PrivateMsgResponse;
import org.hikari.qqbot.client.exception.MissingParamException;

import java.lang.reflect.Type;

/**
 * created at 2024-12-17 11:03:56
 *
 * @author yinchao
 * @version 1.0
 **/
@RequiredArgsConstructor
public class HttpMessageClient implements IMessageClient {

    private final HttpMessageClientConfig config;

    private static final String PATH_SEND_PRIVATE_MSG = "send_private_msg";
    private static final String PATH_SEND_GROUP_MSG = "send_group_msg";

    @Override
    public LLR<PrivateMsgResponse> sendPrivateMsg(PrivateMsgRequest request) {
        Assert.notNull(request, () -> new MissingParamException("Request is null"));
        Assert.notNull(request.getUser_id(), () -> new MissingParamException("Request param 'userId' is null"));
        Assert.notEmpty(request.getMessage(), () -> new MissingParamException("Request param 'messageSegments' is null"));
        return doPost(PATH_SEND_PRIVATE_MSG, request, PrivateMsgResponse.class);
    }

    @Override
    public LLR<GroupMsgResponse> sendGroupMsg(GroupMsgRequest request) {
        Assert.notNull(request, () -> new MissingParamException("Request is null"));
        Assert.notNull(request.getGroup_id(), () -> new MissingParamException("Request param 'groupId' is null"));
        Assert.notEmpty(request.getMessage(), () -> new MissingParamException("Request param 'messageSegments' is null"));
        return doPost(PATH_SEND_GROUP_MSG, request, GroupMsgResponse.class);
    }

    private <T> LLR<T> doPost(String path, Object request, Class<T> dataType) {
        try (HttpResponse response = createPostRequest(path)
                .body(request instanceof String s ? s : JSONUtil.toJsonStr(request))
                .execute()) {
            String bodyStr = response.body();
            ParameterizedTypeImpl type = new ParameterizedTypeImpl(new Type[]{dataType}, null, LLR.class);
            return JSONUtil.toBean(bodyStr, type, false);
        }
    }

    private HttpRequest createPostRequest(String path) {
        String fullPath = getFullUrl(path);
        return HttpUtil.createPost(fullPath)
                .setReadTimeout(config.getReadTimeout())
                .setConnectionTimeout(config.getConnectTimeout())
                .contentType(ContentType.JSON.getValue());
    }

    private String getFullUrl(String path) {
        if (config.getUrl().endsWith("/")) {
            return config.getUrl() + path;
        } else {
            return config.getUrl() + "/" + path;
        }
    }

}
